package com.magictower.primary.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.magictower.primary.R;
import com.magictower.primary.common.SPConstant;
import com.magictower.primary.utils.MD5Utils;

import net.youmi.android.os.OffersManager;

public class HomeActivity extends AppCompatActivity {

    private GridView gv;
    private String[] tvTitle = new String[]{"手机防盗", "通讯卫士", "软件管理", "进程管理",
            "流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心"};
    private int[] ivPic = new int[]{R.drawable.home_safe,
            R.drawable.home_callmsgsafe, R.drawable.home_apps,
            R.drawable.home_taskmanager, R.drawable.home_netmanager,
            R.drawable.home_trojan, R.drawable.home_sysoptimize,
            R.drawable.home_tools, R.drawable.home_settings};
    private SharedPreferences mPref;
    private TextView tv_title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //使用积分墙功能之前进行初始化
        OffersManager.getInstance(this).onAppLaunch();

        mPref = getSharedPreferences(SPConstant.CONFIG, MODE_PRIVATE);

        gv = (GridView) findViewById(R.id.gv_home);
        gv.setAdapter(new HomeAdapter());

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        //手机防盗
                        showDialog();
                        break;
                    case 1:
                        //黑名单管理
                        startActivity(new Intent(HomeActivity.this, CallSafe2Activity.class));
                        break;
                    case 2:
                        //程序管理
                        startActivity(new Intent(HomeActivity.this, AppManagerActivity.class));
                        break;
                    case 3:
                        //进程管理
                        startActivity(new Intent(HomeActivity.this, TaskManagerActivity.class));
                        break;
                    case 4:
                        //流量统计
                        startActivity(new Intent(HomeActivity.this, TrafficManagerActivity.class));
                        break;
                    case 5:
                        //病毒查杀
                        startActivity(new Intent(HomeActivity.this, AntivirusActivity.class));
                        break;
                    case 6:
                        //缓存清理
                        startActivity(new Intent(HomeActivity.this, CleanCacheActivity.class));
                        break;
                    case 7:
                        //高级-查询归属地
                        startActivity(new Intent(HomeActivity.this, AToolsActivity.class));
                        break;
                    case 8:
                        //设置中心
                        startActivity(new Intent(HomeActivity.this, SettingActivity.class));
                        break;
                    default:
                        break;
                }
            }
        });

        //点击显示标题
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
                builder.setTitle("开心一刻！");
                builder.setMessage("\t\t" + tv_title.getText() + "\n\n哈哈哈...\t\t@KJZ");
                builder.show();
            }
        });
        ////有米广告全屏
        //OffersManager.getInstance(this).showOffersWall();
        //
        //// 同时本方法还支持以下重载
        //// 自 Youmi Android OfferWall SDK v5.0.0 起, 支持全屏积分墙退出监听回调
        //OffersManager.getInstance(this).showOffersWall(new Interface_ActivityListener() {
        //    @Override
        //    public void onActivityDestroy(Context context) {
        //        System.out.println("这个是回调的方法");
        //    }
        //});

//        OffersManager.getInstance(this).showOffersWallDialog(this);
//        // 同时本方法还支持以下重载
//        // 传入对话框关闭监听器
//        OffersManager.getInstance(this).showOffersWallDialog(this,new OffersWallDialogListener(){
//            @Override
//            public void onDialogClose() {
//                System.out.println("这个是回调的方法已关闭");
//            }
//        });
//        // 传入对话框宽高像素
//        final int winWidth = getWindowManager().getDefaultDisplay().getWidth() - 60;
//        final int winHeight = getWindowManager().getDefaultDisplay().getHeight() - 120;
//        //OffersManager.getInstance(this).showOffersWallDialog(this, winWidth, winHeight);
//        // 传入对话框宽高像素以及关闭监听器
//        OffersManager.getInstance(this).showOffersWallDialog(this, winWidth, winHeight,
//                new OffersWallDialogListener(){
//                    @Override
//                    public void onDialogClose() {
//                        System.out.println("这个是回调的方法已关闭");
//                    }
//                });
//        // 传入对话框宽高所占屏幕比例（0~1）
//        OffersManager.getInstance(this).showOffersWallDialog(this , 0.9F, 0.9F);
//        // 传入对话框宽高所占屏幕比例（0~1）以及关闭监听器
//        OffersManager.getInstance(this).showOffersWallDialog(this , 0.9F, 0.9F,
//                new OffersWallDialogListener(){
//                    @Override
//                    public void onDialogClose() {
//                        System.out.println("这个是回调的方法已关闭");
//                    }
//                });
    }

    private void showDialog() {
        String password = mPref.getString(SPConstant.PASSWORD_LOST_FIND, "");
        System.out.println("password: " + password);
        if (TextUtils.isEmpty(password)) {
            showResetPassword();
        } else {
            showInputPassword();
        }
    }

    private void showInputPassword() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_input_password, null);
        alertDialog.setView(view, 0, 0, 0, 0);

        final EditText etPassword = (EditText) view.findViewById(R.id.et_password);
        Button confirm = (Button) view.findViewById(R.id.btn_confirm);
        Button cancel = (Button) view.findViewById(R.id.btn_cancel);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = mPref.getString(SPConstant.PASSWORD_LOST_FIND, "");
                String currentPassword = MD5Utils.encode(etPassword.getText().toString());
                if (currentPassword.equals(password)) {
                    startActivity(new Intent(HomeActivity.this, LostFindActivity.class));
                    alertDialog.dismiss();
                } else {
                    Toast.makeText(HomeActivity.this, "密码错误或为空", Toast.LENGTH_SHORT).show();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();

    }

    private void showResetPassword() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_set_password, null);
        alertDialog.setView(view, 0, 0, 0, 0);

        final EditText etPassword1 = (EditText) view.findViewById(R.id.et_password1);
        final EditText etPassword2 = (EditText) view.findViewById(R.id.et_password2);

        Button confirm = (Button) view.findViewById(R.id.btn_confirm);
        Button cancel = (Button) view.findViewById(R.id.btn_cancel);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password1 = etPassword1.getText().toString();
                String password2 = etPassword2.getText().toString();
                if (TextUtils.isEmpty(password1) || TextUtils.isEmpty(password2)) {
                    Toast.makeText(HomeActivity.this, "密码还没输入哦", Toast.LENGTH_SHORT).show();
                } else {
                    if (password1.equals(password2)) {
                        password1 = MD5Utils.encode(password1);
                        mPref.edit().putString(SPConstant.PASSWORD_LOST_FIND, password1).apply();
                        startActivity(new Intent(HomeActivity.this, LostFindActivity.class));
                        alertDialog.dismiss();
                    } else {
                        Toast.makeText(HomeActivity.this, "密码错误不一致", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        alertDialog.show();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OffersManager.getInstance(this).onAppExit();
    }

    class HomeAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return tvTitle.length;
        }

        @Override
        public Object getItem(int position) {
            return tvTitle[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = View.inflate(HomeActivity.this, R.layout.item_home_list, null);
            ImageView iv = (ImageView) view.findViewById(R.id.iv_item);
            TextView tv = (TextView) view.findViewById(R.id.tv_item);
            iv.setImageResource(ivPic[position]);
            tv.setText(tvTitle[position]);

            return view;
        }
    }
}
